<?php
FLEA::loadClass('Controller_Base');

class Controller_Budget extends Controller_Base
{
	function	actionGetBudgets()
	{
		$id		=	$_POST['ID'];
		$type	=	$_POST['Type'];
		
		$id		=	$id==''?0:$id;
		$type	=	$type==''?0:$type;
		
		$_modelBid	=&	FLEA::getSingleton('Model_Bid');
		$budgets	=	$_modelBid->findAllBudgets($id,$type);
		
		if(is_array($budgets))
		{
			$this->purse($budgets,'budgets',0);
		}
	}
	
	function	actionRequestBid()
	{
		$type	=	$_POST['Type'];
		$type	=	$type==''?0:$type;
		
		$id		=	$_POST['ID'];
		$id		=	$id==''?0:$id;
		
		$targetDate	=	date("Y-m-d",strtotime($_POST['FillingDate']));
		
		switch($type)
		{
			case 0:
				$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
				
				$invention['InventionID']	=	$id;
				$invention['TargetDate']	=	$targetDate;
				$invention['RequestBidDate']=	date("Y-m-d",time());
				$invention['BBid']			=	1;
				$invention['MaxBudget']		=	$_POST['MaxBudget'];
				$invention['ActionDueDate']	=	$invention['TargetDate'];
				$result			=	$_modelInvention->save($invention);	
				break;
			case 1:
				$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
				
				$mark['TradeMarkID']	=	$id;
				$mark['TargetDate']		=	$targetDate;
				$mark['RequestBidDate']	=	date("Y-m-d",time());
				$mark['BBid']			=	1;
				$mark['MaxBudget']		=	$_POST['MaxBudget'];
				
				$result			=	$_modelTradeMark->save($mark);	
				break;
			case 2:
				$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
				
				$copy['CopyrightID']	=	$id;
				$copy['TargetDate']		=	$targetDate;
				$copy['RequestBidDate']	=	date("Y-m-d",time());
				$copy['BBid']			=	1;
				$copy['MaxBudget']		=	$_POST['MaxBudget'];
				
				$result			=	$_modelCopyright->save($copy);	
				break;
		}

		$this->showResult($result);
	}
	
	function	actionSetBudget()
	{
		
		$_modelBid	=&	FLEA::getSingleton('Model_Bid');
		
		$type	=	$_POST['Type'];
		$type	=	$type==''?0:$type;
		
		$id		=	$_POST['ID'];
		$id		=	$id==''?0:$id;

		
		$bid['ID']		=	$id;
		$bid['Type']	=	$type;
		$bid['Budget']	=	$_POST['Budget'];
		$bid['FirmID']	=	$_POST['FirmID'];
		
		$result	=	$_modelBid->saveBid($bid);
		
		$this->showResult($result);
	}
	
	function 	actionAssignToFirm()
	{
		$type	=	$_POST['Type'];
		$type	=	$type==''?0:$type;
		
		$id		=	$_POST['ID'];
		$id		=	$id==''?0:$id;
		
		$firmID	=	$_POST['FirmID'];
		$firmID	=	$firmID==''?0:$firmID;
		
		switch($type)
		{
			case 0:
				$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
				$invention['InventionID']	=	$id;
				$invention['FirmID']		=	$firmID;
				$invention['BConfirm']		=	3;
				$result			=	$_modelInvention->save($invention);	
				break;
			case 1:
				$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
				
				$mark['TradeMarkID']	=	$id;
				$mark['FirmID']			=	$firmID;
				$mark['BConfirm']		=	3;
				$result			=	$_modelTradeMark->save($mark);	
				break;
			case 2:
				$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
				
				$copy['CopyrightID']	=	$id;
				$copy['FirmID']			=	$firmID;
				$copy['BConfirm']		=	3;
				$result			=	$_modelCopyright->save($copy);	
				break;
		}
		
		$this->showResult($result);
	}
	
	function 	actionReturnCase()
	{
		$type	=	$_POST['Type'];
		$type	=	$type==''?0:$type;
		
		$id		=	$_POST['ID'];
		$id		=	$id==''?0:$id;
		
		switch($type)
		{
			case 0:
				$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
				$invention['InventionID']	=	$id;
				$invention['BConfirm']		=	2;
				$result			=	$_modelInvention->save($invention);	
				break;
			case 1:
				$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
				
				$mark['TradeMarkID']	=	$id;
				$mark['BConfirm']	=	2;
				$result			=	$_modelTradeMark->save($mark);	
				break;
			case 2:
				$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
				
				$copy['CopyrightID']	=	$id;
				$copy['BConfirm']		=	2;
				
				$result			=	$_modelCopyright->save($copy);	
				break;
		}
		
		$this->showResult($result);
	}
}
?>